const app = getApp()
let store = require('../storage.js')
let system = store.getSystemInfo();
const clientInfo = {
  'clientType': 'mp',
  'appnm': 'imoocpay',
  'model': system.model,
  'os': system.system,
  'screen': system.screenWidth + '*' + system.screenHeight,
  'version': App.version,
  'channel': 'miniprogram'
}
const errMsg = '服务异常，请稍后重试';

function networkget(url, header = {}, data = {}, params = {}, option = {}) {
  let { loading = true, toast = true, isMock = false, method = 'get' } = option;
  let promise = new Promise(function (resolve, reject) {
    if (loading) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    }
    let env = App.config
    wx.request({
      url: env + url,
      header,
      data,
      method,
      success: function (result) {
        let res = result.data;
        console.log(res)
        if (res.code === '200') {
          if (loading) {
            wx.hideLoading();
          }
          resolve(res.data);
        } else {
          wx.hideLoading();
          console.log(res)
          reject(res);
        }
      },
      fail: function (e = { code: -1, msg: errMsg, errMsg }) {
        let msg = e.errMsg;
        if (msg == 'request:fail timeout') {
          msg = '服务请求超时，请稍后处理';
        }
        wx.showToast({
          title: msg,
          icon: 'none'
        })
        reject(e);
      }
    })
  });
  return promise;
}

function networkpost(url, header = {}, data = {}, params = {}, option = {}) {
  let { loading = true, toast = true, isMock = false, method = 'post' } = option;
  let promise = new Promise(function (resolve, reject) {
    if (loading) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    }
    let env = App.config
    wx.request({
      url: env + url + params,
      header,
      data: data,
      method,
      success: function (result) {
        let res = result.data;
        console.log(res)
        if (res.code === '200') {
          console.log('=======success=======')
          if (loading) {
            wx.hideLoading();
          }
          resolve(res.data);
        } else {
          console.log('=======fail=======')
          wx.hideLoading();
          console.log(res)
          reject(res);
        }
      },
      fail: function (e = { code: -1, msg: errMsg, errMsg }) {
        let msg = e.errMsg;
        if (msg == 'request:fail timeout') {
          msg = '服务请求超时，请稍后处理';
        }
        wx.showToast({
          title: msg,
          icon: 'none'
        })
        reject(e);
      }
    })
  });
  return promise;
}

function networkdelete(url, header = {}, data = {}, params = {}, option = {}) {
  let { loading = true, toast = true, isMock = false, method = 'delete' } = option;
  let promise = new Promise(function (resolve, reject) {
    if (loading) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    }
    let env = App.config
    wx.request({
      url: env + url + params,
      header,
      data: data,
      method,
      success: function (result) {
        let res = result.data;
        console.log(res)
        if (res.code === '200') {
          console.log('=======success=======')
          if (loading) {
            wx.hideLoading();
          }
          resolve(res.data);
        } else {
          console.log('=======fail=======')
          wx.hideLoading();
          console.log(res)
          reject(res);
        }
      },
      fail: function (e = { code: -1, msg: errMsg, errMsg }) {
        let msg = e.errMsg;
        if (msg == 'request:fail timeout') {
          msg = '服务请求超时，请稍后处理';
        }
        wx.showToast({
          title: msg,
          icon: 'none'
        })
        reject(e);
      }
    })
  });
  return promise;
}

function networkput(url, header = {}, data = {}, params = {}, option = {}) {
  let { loading = true, toast = true, isMock = false, method = 'put' } = option;
  let promise = new Promise(function (resolve, reject) {
    if (loading) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    }
    let env = App.config
    wx.request({
      url: env + url + params,
      header,
      data: data,
      method,
      success: function (result) {
        let res = result.data;
        console.log(res)
        if (res.code === '200') {
          console.log('=======success=======')
          if (loading) {
            wx.hideLoading();
          }
          resolve(res.data);
        } else {
          console.log('=======fail=======')
          wx.hideLoading();
          console.log(res)
          reject(res);
        }
      },
      fail: function (e = { code: -1, msg: errMsg, errMsg }) {
        let msg = e.errMsg;
        if (msg == 'request:fail timeout') {
          msg = '服务请求超时，请稍后处理';
        }
        wx.showToast({
          title: msg,
          icon: 'none'
        })
        reject(e);
      }
    })
  });
  return promise;
}

module.exports = {
  get: networkget,
  post: networkpost,
  delete: networkdelete,
  put: networkput
}